const app = getApp();
const $api = require('../../utils/api.js').API;
Page({
    data: {
        isLogin: false,
        userinfo: {},
        publishOrder: [],
        collectOrder: [],
        takingOrder: []
    },

    onLoad() {
        console.log('my onload')
        this.setData({
            isLogin: app.globalData.isLogin,
        })
        if (this.data.isLogin === true) {
            this.loadUserinfo(app.globalData.openid)
        }
    },

    onShow() {
        console.log('my onshow')
        this.getPublish()
        this.getCollect()
        this.getTaking()
        this.loadUserinfo(app.globalData.openid)
    },

    // 登录
    login() {
        let _this = this
        console.log('login')
        wx.login({
            success(res) {
                console.log(res)
                if (res.code) {
                    //发起网络请求
                    $api.login(res.code)
                        .then(res => {
                            let openid = res.data.openid
                            app.globalData.openid = openid
                            wx.setStorage({
                                key: "openid",
                                data: openid,
                                success() {
                                    // console.log(res)
                                    _this.getPublish()
                                    _this.getTaking()
                                    _this.getCollect()
                                }
                            })
                            _this.setData({
                                isLogin: true
                            })
                            _this.loadUserinfo(openid)

                        })
                        .catch(err => console.log(err))
                } else {
                    console.log('登录失败！' + res.errMsg)
                }
            }
        })
    },

    // 加载用户信息
    loadUserinfo(params) {
        $api.getUserinfo({ openid: params })
            .then(res => {
                let userinfo = res.data.data[0]
                this.setData({
                    userinfo
                })
            })
            .catch(err => console.log(err))
    },

    // 跳转到编辑页面
    toUserinfo() {
        let userinfo = JSON.stringify(this.data.userinfo)
        wx.navigateTo({
            url: `/pages/userinfo/userinfo?userinfo=${userinfo}`,
        });
    },

    // 获取发布的订单
    getPublish() {
        $api.getPublish({ openid: app.globalData.openid })
            .then(res => {
                this.setData({
                    publishOrder: res.data.data
                })
            })
            .catch(err => console.log(err))

    },

    // 获取收藏的订单
    getCollect() {
        $api.getCollect({ openid: app.globalData.openid })
            .then(res => {
                this.setData({
                    collectOrder: res.data.data
                })
            })
            .catch(err => console.log(err))
    },

    // 获取接单
    getTaking() {
        $api.getTaking({ openid: app.globalData.openid })
            .then(res => {
                this.setData({
                    takingOrder: res.data.data
                })
            })
            .catch(err => console.log(err))
    },

    // 跳转到小程序评分
    toGrade() {
        wx.navigateTo({
            url: `/pages/rate/rate?openid=${app.globalData.openid}`,
        });
    },

    // 跳转到我发布的订单
    toPublish() {
        let publishOrder = JSON.stringify(this.data.publishOrder)
        wx.navigateTo({
            url: `/pages/PCT/PCT?order=${publishOrder}&type=0`
        })
    },

    // 跳转到我收藏的订单
    toCollect() {
        let collectOrder = JSON.stringify(this.data.collectOrder)
        wx.navigateTo({
            url: `/pages/PCT/PCT?order=${collectOrder}&type=2`,
        });
    },

    // 跳转到我的接单
    toTake() {
        let takingOrder = JSON.stringify(this.data.takingOrder)
        wx.navigateTo({
            url: `/pages/PCT/PCT?order=${takingOrder}&type=1`,
        });
    }

})